package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.PlaylistMusic;
import com.softeem.music_sys.service.PlaylistMusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 歌单音乐关系控制器
 */
@Tag(name = "歌单音乐接口", description = "歌单音乐关系相关操作:添加、删除、查询等")
@RestController
@RequestMapping("/playlist-music")
public class PlaylistMusicController {
    
    @Autowired
    private PlaylistMusicService playlistMusicService;

    @Operation(summary = "添加音乐到歌单", description = "添加音乐到指定歌单")
    @PostMapping
    public Result add(@RequestBody PlaylistMusic playlistMusic) {
        return playlistMusicService.addMusicToPlaylist(playlistMusic);
    }

    @Operation(summary = "从歌单移除音乐", description = "从歌单中移除指定音乐")
    @Parameters({
            @Parameter(name = "playlistId", description = "歌单ID"),
            @Parameter(name = "musicId", description = "音乐ID")
    })
    @DeleteMapping
    public Result remove(@RequestParam Integer playlistId, @RequestParam Integer musicId) {
        return playlistMusicService.removeMusicFromPlaylist(playlistId, musicId);
    }

    @Operation(summary = "删除关系记录", description = "根据ID删除歌单音乐关系")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return playlistMusicService.deletePlaylistMusic(id);
    }

    @Operation(summary = "获取歌单的所有音乐", description = "根据歌单ID获取其包含的所有音乐")
    @GetMapping("/playlist/{playlistId}")
    public Result getByPlaylist(@PathVariable Integer playlistId,
                                @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit) {
        Page<PlaylistMusic> resultPage = playlistMusicService.getMusicsByPlaylist(playlistId, page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "获取音乐所在的歌单", description = "根据音乐ID获取包含该音乐的所有歌单")
    @GetMapping("/music/{musicId}")
    public Result getByMusic(@PathVariable Integer musicId,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit) {
        Page<PlaylistMusic> resultPage = playlistMusicService.getPlaylistsByMusic(musicId, page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "批量添加音乐到歌单", description = "批量添加多首音乐到指定歌单")
    @PostMapping("/batch")
    public Result batchAdd(@RequestParam Integer playlistId, @RequestBody List<Integer> musicIds) {
        return playlistMusicService.batchAddMusicToPlaylist(playlistId, musicIds);
    }

    @Operation(summary = "清空歌单", description = "删除歌单中的所有音乐")
    @DeleteMapping("/clear/{playlistId}")
    public Result clear(@PathVariable Integer playlistId) {
        return playlistMusicService.clearPlaylist(playlistId);
    }
}